//
//  JKBaseTableViewCell.swift
//  ZhiHuDaily-Swift
//
//  Created by albert on 2018/3/9.
//  Copyright © 2018年 安永博. All rights reserved.
//

import UIKit
import SnapKit

class JKBaseTableViewCell: UITableViewCell {
    
    public var skinModel: String {
        
        return UserDefaults.standard.string(forKey: JKCurrentSkinModelKey) ?? JKDaySkinModelValue
    }
    
    /** 选中背景view */
    public lazy var selectedBgView: UIView = {
        
        let selectedBgView = UIView()
        
        selectedBgView.backgroundColor = JKSameRGBColor(217)
        
        return selectedBgView
    }()
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        selectedBgView.frame = bounds
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    // MARK: - 初始化
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initialization()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        initialization()
    }
    
    /** 初始化 子类注意调用super */
    public func initialization()  {
        
        selectedBackgroundView = selectedBgView
        
        NotificationCenter.default.addObserver(self, selector: #selector(updateSkin), name: NSNotification.Name(rawValue: JKSkinModelDidChangedNotification), object: nil)
    }
    
    override func didMoveToSuperview() {
        super.didMoveToSuperview()
        
        if superview == nil { return }
        
        updateSkin()
    }
    
    deinit {
        
        NotificationCenter.default.removeObserver(self)
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension JKBaseTableViewCell: JKUpdateSkinProtocol {
    
    func updateSkin() {
        
        backgroundColor = (skinModel == JKNightSkinModelValue) ? JKNightSkinBgColor: JKGlobalBgColor
        
        self.selectedBgView.backgroundColor = (skinModel == JKNightSkinModelValue) ?  JKSameRGBColor(40) : JKSameRGBColor(217)
    }
}
